#include "simple_protocol.h"
#include <string.h>

/// @brief 设置栈区的协议结构体
/// @param ptr simple_protocol_t 变量地址
/// @param id 消息id
/// @param packs_len 分包长度
/// @param pack 分包号
/// @param auth 鉴权号(12位)
/// @param data 消息内容(256位)
/// @return
int simple_protocol_set(simple_protocol_t *ptr, uint8_t id, uint8_t packs_len, uint8_t pack, const int8_t *auth, const int8_t *data)
{
    if (ptr == NULL)
        return -1;
    // 清空旧数据
    memset(ptr, 0, sizeof(simple_protocol_t));
    int auth_len = strlen(auth);
    if (auth_len <= 0 || auth_len > SIMPLE_PROTOCOL_AUTH_LEN)
        return -1;
    int data_len = strlen(data);
    if (data_len <= 0 || data_len > (SIMPLE_PROTOCOL_DATA_LEN - 1))
        return -1;

    ptr->head = SIMPLE_PROTOCOL_HEAD;
    ptr->id = id;
    ptr->packs_len = packs_len;
    ptr->pack = pack;
    strncpy(ptr->auth, auth, auth_len);
    ptr->len = data_len;
    strncpy(ptr->data, data, data_len);
    ptr->saved = 0;
    ptr->tail = SIMPLE_PROTOCOL_TAIL;
    return 0;
}

/// @brief 设置不分包栈区的协议结构体
/// @param ptr simple_protocol_t 变量地址
/// @param id 消息id
/// @param auth 鉴权号(12位)
/// @param data 消息内容(256位)
/// @return
int simple_protocol_nopack_set(simple_protocol_t *ptr, uint8_t id, const int8_t *auth, const int8_t *data)
{

    if (ptr == NULL)
        return -1;
    // 清空旧数据
    memset(ptr, 0, sizeof(simple_protocol_t));
    int auth_len = strlen(auth);
    if (auth_len <= 0 || auth_len > SIMPLE_PROTOCOL_AUTH_LEN)
        return -1;
    int data_len = strlen(data);
    if (data_len <= 0 || data_len > (SIMPLE_PROTOCOL_DATA_LEN - 1))
        return -1;

    ptr->head = SIMPLE_PROTOCOL_HEAD;
    ptr->id = id;
    ptr->packs_len = SIMPLE_PROTOCOL_NOPACK;
    ptr->pack = SIMPLE_PROTOCOL_NOPACK;
    strncpy(ptr->auth, auth, auth_len);
    ptr->len = data_len;
    strncpy(ptr->data, data, data_len);
    ptr->saved = 0;
    ptr->tail = SIMPLE_PROTOCOL_TAIL;
    return 0;
}

/// @brief simple_protocol全局回调函数数组
#define SIMPLE_PROTOCOL_GLOBAL_CB_LEN 16
static simple_protocol_cb_impl simple_protocol_global_cb[SIMPLE_PROTOCOL_GLOBAL_CB_LEN] = {0};

/// @brief 设置消息id对应的回调函数
/// @param id 消息id
/// @param func 函数
/// @return
int simple_protocol_global_cb_set(uint8_t id, simple_protocol_cb_impl func)
{
    if (id > (SIMPLE_PROTOCOL_GLOBAL_CB_LEN - 1))
        return -1;
    simple_protocol_global_cb[id] = func;
    return 0;
}

/// @brief 调用指定回调函数
/// @param ptr simple_protocol_t变量地址
/// @param userdata 用户数据地址
/// @return
int simple_protocol_global_cb_run(simple_protocol_t *ptr, void *userdata)
{
    if (ptr == NULL || ptr->id > (SIMPLE_PROTOCOL_GLOBAL_CB_LEN - 1))
        return -1;
    if (simple_protocol_global_cb[ptr->id] != NULL)
        return simple_protocol_global_cb[ptr->id](ptr, userdata);
    return 0;
}

/// @brief 调用自定义回调函数
/// @param ptr simple_protocol_t变量地址
/// @param func 用户函数
/// @param userdata 用户数据地址
/// @return 用户函数返回值
int simple_protocol_run(simple_protocol_t *ptr, simple_protocol_cb_impl func, void *userdata)
{
    if (ptr == NULL || func == NULL)
        return -1;
    return func(ptr, userdata);
}